var myFunc = require("./myCommonFunction.js");
var widthG = device.width;
var heightG = device.height
var findtime = 5000;
var start = false;

var list = [];
var w = floaty.window(
    <vertical>
        <button id="look" text="浏览" />
        <button id="ok" text="抽奖" />
        <button id="jiang" text="奖励任务" />
        <button id="stop" text="停止" />
        <button id="xiaochengxu" text="小程序" />
        <button id="hbTask" text="红包任务" />
    </vertical>
);

// w.setSize(600, 600);
// w.setTouchable(true);
w.setAdjustEnabled(true);
w.setPosition(0, 200);
w.exitOnClose();

var taskWord = '每做一次任务就可以拆一个快递箱哦~';
w.look.click(function () {
    var t = threads.start(function () {
        liulan();
    });
    list.push(t);
});


w.xiaochengxu.click(function () {
    xiaochengxu();
});

w.jiang.click(function () {
    jiangli();
});

w.hbTask.click(function () {
    var t = threads.start(function () {
        while(myFunc.clickTextByBtn('去完成')) {
            sleep(8000)
            myFunc.tuichuToStr('去邀请')
            sleep(1000)
        }
    });
    list.push(t);
});

w.ok.click(function () {
    var t = threads.start(function () {
        // while (true) {
        //     myFunc.textCpClick('抽奖');
        //     if (textContains('金币不足').findOne(1000) != null) {
        //         break;
        //     }
        //     textContains("获得奖励").findOne(3000);
        //     // click(widthG - 100, heightG * 0.24);
        //     sleep(1000);
        // }
        var a = myFunc.find('点我领');
        a.forEach(element => {
            myFunc.pClick(element)
            sleep(1000)
        });
    });
    list.push(t);
});

w.stop.click(function () {
    for (var element of list) {
        log(element)
        if (element != null) {
            element.interrupt();
        }
    }
    list = [];
});


setInterval(() => { }, 1000);


function jing() {
    try {
        myFunc.openMyApp("京东");
        myFunc.log("DA KAI LE MEIYOU")
        myFunc.clickCenter(desc("浮层活动").findOne(findtime));
        sleep(1000);
        textContains("汪汪币").findOne(findtime);
        click(1226, 886);
        sleep(1000);
        click(1250, 2070);
        textContains("任务奖励").findOne(findtime);

    } catch (error) {
        log(error)
        myFunc.tuichu("京东");
        jing();
    }
}

function liulan() {
    try {
        liulan1();
    } catch (error) {
        console.log(error);
        myFunc.tuichuToStr(taskWord);
        liulan();
    }
}

function bianli2(a, list) {
    if (a.childCount() > 0) {
        var child = a.children();
        for (var element of child) {
            if (element != null) {
                var text = element.text();
                if (text.length > 0) {
                    // log(text, start)
                    if (!start && text.indexOf('去分享') > -1) {
                        start = true;
                    }
                    if (start) {
                        list.push(element);
                    }
                    // sleep(300)
                }
                this.bianli2(element, list, start);
            }
        };
    }
}


function liulan1() {
    log("开始")

    if (text(taskWord).findOnce() != null) {
        //点关闭
        click(1012, 729);
        sleep(2000);
    }
    //点任务
    click(548, 1746);
    sleep(2000);
    text(taskWord).findOne(2000);
    sleep(2000);
    // 'text:邀请好友助力(0/8)' 'desc:null' 'idx:1' 'depth:19'
    //  'text:每邀1个好友可得10000金币' 'desc:null' 'idx:2' 'depth:19'
    //  'text:' 'desc:null' 'idx:3' 'depth:19'
    var a = className("android.widget.FrameLayout").findOnce();
    var list = [];
    start = false;
    bianli2(a, list);
    log(list.length)
    // var task = className("android.view.View").textMatches(".*\\(\\d{1,2}/\\d{1,2}\\)").find();
    // var task = className("android.view.View").boundsInside(0, 1200, 1440, 3000).indexInParent(1).depth(19).find();

    // var task1 = task[0];
    // var desc = className(task1.className()).indexInParent(task1.indexInParent() + 1).depth(task1.depth()).find();
    // var desc = className("android.view.View").boundsInside(0, 1200, 1440, 3000).indexInParent(2).depth(19).find();

    // var statusBut = className("android.view.View").indexInParent(task1.indexInParent() + 2).depth(task1.depth()).find();
    // var statusBut = className("android.view.View").boundsInside(0, 1200, 1440, 3000).indexInParent(3).depth(19).find();
    // var statusBut = className("android.view.View").text('去完成').find();

    // myFunc.log(task.length, desc.length, statusBut.length);

    // if (task.length == 0 && text('累计任务奖励').findOnce() == null) {
    //     throw "返回";
    // }
    // var dica = desc.length - statusBut.length;
    for (let index = 1; index < list.length; index++) {
        var descText = '';
        var taskText = '';
        if (list[index].text().indexOf('去完成') > -1 && list[index + 1].text().indexOf('去完成') > -1) {
            descText = list[index - 1].text();
            taskText = list[index - 3].text() + list[index - 2].text();
        } else {
            continue;
        }

        myFunc.log(index, taskText, descText);

        if (taskText.indexOf("小程序") != -1 || taskText.indexOf("邀请") != -1 || taskText.indexOf("城城") != -1
            || taskText.indexOf("入会") != -1 || taskText.indexOf("会员") != -1 || descText.indexOf("入会") != -1
            || descText.indexOf("下单") != -1
        ) {
            continue;
        }
        var numIdx = myFunc.getNumLimit(taskText);
        if (numIdx[0] == numIdx[1]) {
            continue;
        }

        if (taskText.indexOf("去浏览") != -1) {
            myFunc.log("去浏览 4次");
            for (let y = numIdx[0]; y - numIdx[1] < 0; y++) {
                myFunc.clickCenter(list[index]);
                if (textContains("点击浏览").findOne(5000) != null) {
                    var s = className("android.view.View").indexInParent(3).depth(18).clickable().find();
                    // var s = idContains("jmdd-react-smash").find()
                    for (let idx = 0; idx < 4; idx++) {
                        sleep(1000);
                        var element = s[idx];
                        // log(idx, element)
                        element.click();
                        sleep(1000);
                        back();
                    }
                    sleep(2000);
                    back();
                    // textContains("任务奖励").findOne(findtime);
                    sleep(1500);
                }
            }
            liulan1();
            return;
        }
        if (descText.indexOf("加购") != -1) {
            myFunc.log("加购 4次");
            // for (let y = numIdx[0]; y - numIdx[1] < 0; y++) {
            // log("加购", y);
            myFunc.clickCenter(list[index]);
            sleep(1000);
            if (textContains("加购").findOne(5000) != null) {
                var s = className("android.view.View").indexInParent(3).depth(18).clickable().find();
                // var s = idContains("jmdd-react-smash").find()
                for (let idx = 0; idx < 4; idx++) {

                    var element = s[idx];
                    // log(idx, element)
                    element.click();
                    myFunc.waitToNotFind('完成拆1个');
                    sleep(1000);
                    myFunc.tuichuToStr('完成拆1个');
                    sleep(1500);
                }
                sleep(1000);
                myFunc.tuichuToStr(taskWord);
                // textContains("任务奖励").findOne(findtime);
                sleep(4000);
                log("回任务");
            }
            // }
            liulan1();
            return;
        }
        if (descText.indexOf("浏览") != -1 && descText.indexOf("8s") == -1) {
            myFunc.log(descText, "浏览一下", taskText.indexOf('种草'));

            for (let y = numIdx[0]; y - numIdx[1] < 0; y++) {
                myFunc.clickCenter(list[index]);
                sleep(2000);
                if (taskText.indexOf('种草') != -1) {
                    for (let idx = 0; idx < 4; idx++) {
                        // var s = className("android.view.View").indexInParent(2).depth(4).findOnce();
                        // s.click();
                        myFunc.findOne('喜欢')
                        click(782, 1887);
                        sleep(1500);
                        back();
                        sleep(3000);
                    }
                    myFunc.tuichuToStr(taskWord);
                    break;
                } else {
                    myFunc.tuichuToStr(taskWord);
                }
                sleep(1500);
            }


            liulan1();
            return;

        }
        if (descText.indexOf("浏览") != -1 && descText.indexOf("8s") != -1) {
            myFunc.log("浏览8s");

            for (let y = numIdx[0]; y - numIdx[1] < 0; y++) {
                myFunc.log("浏览", y);
                myFunc.clickCenter(list[index]);
                log("浏览点击");
                sleep(3000);
                swipe(730, 1550, 750, 1200, 500);
                var time = 8000;
                if (textStartsWith("滑动浏览").findOne(4000) == null) {
                    time = time - 4000;
                }
                sleep(2000);
                swipe(730, 1550, 750, 1200, 500);
                sleep(time - 1000);
                textStartsWith("获得").findOne(2000);
                if (taskText.indexOf('品牌墙') != -1) {
                    while (scrollUp()) {

                    }
                    break;
                } else {
                    myFunc.tuichuToStr(taskWord);
                }

                log("回任务");
                sleep(4000);
                //关闭优惠券弹框
                if (myFunc.findOnce('去使用') != null) {
                    click(980, 432)
                    sleep(2000);
                }
                // textContains("助力").findOne(findtime);
            }
            liulan1();
            return;
        }

    }

}

function getQuWanCheng(a) {
    var b = a.bounds();
    var c = 80;
    return text("去完成").boundsInside(960, b.top - c, 1440, b.bottom + c).findOnce();
}


function xiaochengxu(a) {
    var t = threads.start(function () {
        for (let index = 0; index < 10; index++) {
            click(916, 1016)
            sleep(2000);
            back();
            sleep(2000);
        }
    });
    list.push(t);

}

function jiangli(a) {
    var t = threads.start(function () {
        //查抽奖点
        var dian = className("android.view.View").indexInParent(0).depth(17).find();
        var weizhi = [180, 500, 820, 1145, 180, 500, 820, 1145, 635, 940, 1275];
        for (var x = 0; x < dian.length; x++) {
            if (x % 4 == 0 && x > 0) {
                swipe(1350, 2946, 0, 2946, 500);
                sleep(1000);
            }
            click(weizhi[x], 2920);
            text('剩余抽奖次数').findOne(5000);
            //点关注
            click(728, 2290);
            sleep(1000);
            myFunc.tuichuToStr('热爱奇旅');
            sleep(1000);
            //做任务
            click(740, 2514);
            sleep(1000);
            var wan = text('去完成').find();
            if (wan.length == 2) {
                myFunc.log('任务做完');
            } else {
                //其它任务
                myFunc.log('其它任务');
                for (var item of wan) {
                    myFunc.clickCenter(item);
                    sleep(1000);
                    myFunc.tuichuToStr('热爱奇旅');
                }
                swipe(600, 2689, 600, 2300, 400);
                sleep(500);
                if (textContains('2/2').findOnce() == null) {
                    myFunc.log('加购任务');
                    //加购
                    click(1226, 2964);
                    sleep(1000);
                    // var jiagou = className("android.view.View").indexInParent(2).depth(22).find();
                    // if (jiagou.length == 2) {
                    // jiagou[0].click();
                    click(640, 2678)
                    sleep(1500);
                    // jiagou[1].click();
                    click(1307, 2678)
                    sleep(1500);
                    click(1350, 1319)
                    sleep(1000);
                    // }
                }
            }
            click(1350, 1580);
            sleep(1000);
            //开盒子
            myFunc.clickText('开好礼');
            if (textContains('任务').findOne(1000) == null) {
                click(1292, 816)
                sleep(500);
                for (var idx = 0; idx < 6; idx++) {
                    var he = className("android.view.View").boundsInside(100, 1500, 1310, 2230).indexInParent(idx).depth(17).clickable().findOnce();
                    if (he == null) {
                        continue;
                    }
                    he.click();
                    if (textContains('任务').findOne(1000) != null) {
                        myFunc.log('盒子开完');
                        break;
                    }
                    sleep(2000);
                    click(1292, 816);
                }
            }
            back();
            sleep(1000);
        }
    });
    list.push(t);

}











